// pages/business/management/management.js
var shihan = require('../../../common/fetch')
var config = require('../../../common/config')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    listflag: true,
    has_more: true,
    is_child: 1, // 1: 代表我的返货单列表 2: 代表代理商返货单列表
    currentTab: 0,
    isfresh: false,
    showLoading: true,
    agent_return_list: [],
    res_navbar: ['我的返货单', '代理商返货单'],
    reminder: ['你还没有申请过返货', '没有代理向您申请过返货']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.agentReturnList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isfresh) {
      this.setData({
        isfresh: false
      })
      this.onPullDownRefresh();
    }
  },

  onResNavbarTap: function (e) { 
    this.setData({
      page: 1,
      listflag: true,
      showLoading: true,
      agent_return_list: [],
      is_child: e.currentTarget.dataset.index + 1,
      currentTab: e.currentTarget.dataset.index
    });
    this.agentReturnList();
  },

  agentReturnList: function () { //agent_return_list
    var that = this;
    // is_child  必填 1: 代表我的返货单列表 2: 代表代理商返货单列表
    shihan.sendRequest(config.agent_return_list, 'POST', { is_child: that.data.is_child, page: that.data.page, page_size: that.data.page_size })
      .then(function (response) {
        if (response.data.code == 0) {
          if (response.data.data.sh_items.length>0) {
            that.setData({
              agent_return_list: that.data.agent_return_list.concat(response.data.data.sh_items),
            })
            if (response.data.data.sh_items.length == that.data.agent_return_list.length) {
              that.setData({
                listflag: false,
                has_more: false
              })
            }
          } else {
            that.setData({
              listflag: false,
              has_more: false
            })
          }
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh()
      }, function (error) {
        console.log(error);
      });
  },
  cancelTrade: function (e) { //我的返货单列表-取消申请
    var that = this,
      orderno = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: '确定要取消申请吗？',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function (res) {
        if (res.confirm) {//agent_return_cancel
          shihan.sendRequest(config.agent_return_cancel, 'POST', { order_no: orderno})
            .then(function (response) {
              if (response.data.code == 0) {
                shihan.showToast.call(that, '取消成功');
                that.onPullDownRefresh();
              } else {
                shihan.showToast.call(that, response.data.msg);
              }
            }, function (error) {
              console.log(error);
            });
        }
      }
    })
  },

  orderDetail: function (e) { //订单详情
    wx.navigateTo({
      url: '/pages/business/myorderdetail/myorderdetail?index=2&ischild=' + this.data.is_child +'&tradeno=' + e.currentTarget.dataset.order,
    })
  },

  lookLogistic: function (e) { //查看物流信息
    wx.navigateTo({
      url: '/pages/other/viewlogistics/viewlogistics?index=3&id=' + e.currentTarget.dataset.id,
    })
  },

  addLogistic: function (e) { //添加物流信息
    wx.navigateTo({
      url: '/pages/business/addlogistic/addlogistic?index=2&orderid=' + e.currentTarget.dataset.id,
    })
  },

  agentReturn: function () {//申请返货
    wx.navigateTo({
      url: '/pages/business/myagent/myagent?index=2',
    })
  },
  sweepCode: function (e) { //扫码发货，收货
    wx.navigateTo({
      url: '/pages/business/sweepcode/sweepcode?orderid=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.type + '&index=' + e.currentTarget.dataset.index,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      listflag: true,
      has_more: true,
      showLoading: true,
      agent_return_list: []
    });
    this.agentReturnList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})